package com.ruoyi.personal.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.personal.domain.User;
import com.ruoyi.personal.domain.UserPhoneVO;
import com.ruoyi.personal.mapper.PhoneMapper;
import com.ruoyi.personal.service.IPhoneService;
import com.ruoyi.personal.ws.DateEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.ruoyi.personal.ws.SessionNameEnum.PHONE_SESSION;

@Service
public class PhoneServiceImpl implements IPhoneService {
    @Autowired
    private PhoneMapper phoneMapper;

    @Autowired
    DateEndpoint dateEndpoint;

    public List<UserPhoneVO> getAllPhone() {

        List<User> users = phoneMapper.selectPhoneAll();
        List list = users.stream()
            .map(user -> new UserPhoneVO(user.getNumid(),user.getUsername(), user.getPhoneNumber()))
            .collect(Collectors.toList());
        for (Object o : list) {
            System.out.println(o);
        }
        return list;
    }

    @Override
    public Boolean updatePhone(UserPhoneVO userPhone) {
        int i = phoneMapper.updatePhoneNumber(userPhone.getNumid(), userPhone.getPhoneNumber());
        if (i > 0) {
            dateEndpoint.sendMessageToUser(PHONE_SESSION.getName(),  JSON.toJSONString(getAllPhone()));
            return true;
        }else {
            return false;
        }
    }

}
